const express = require('express');
//创建路由对象
const r = express.Router();
//引入数据库
const pool = require('../pool/pool');
const nodemailer = require('nodemailer');
const { generateToken } = require('../core/utils');
/*******************************************/
//1.用户注册接口
r.post('/register', (req, res, next) => {
  let obj = req.body;
  let name = obj.uName;
  let email = obj.uEmail;
  let pwd = obj.uPwd;

  // let sql01 = `SELECT * FORM user WHERE u_email = ?`
  let sql02 = `INSERT INTO user value (?,?,?,?,?)`;
  pool.query(sql02, [, name, email, pwd, ,], (err, data) => {
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '注册成功!' });
    } else {
      res.send({ code: '201', msg: '注册失败!' });
      return;
    }
  });
});
// 2.判断用户邮箱是否已经被注册
r.post('/checkEmail', (req, res, next) => {
  let obj = req.body;
  let email = obj.uEmail;
  sql = `SELECT * FROM user WHERE u_email = "${email}"`;
  pool.query(sql, (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.length == 0) {
      // 未查询到结果 邮箱号没有被注册过
      res.send({ code: '200', msg: '邮箱号未被注册！' });
    } else {
      res.send({ code: '201', msg: '邮箱号已被注册！' });
    }
  });
});
// 3.发送验证码
r.post('/getCode', (req, res, next) => {
  // 生成验证码
  let code = Math.floor(Math.random() * 900000) + 100000;
  // 获取传递进来的邮箱号
  let obj = req.body;
  let email = obj.uEmail;
  let sql = `INSERT INTO user_code value ("${email}","${code}")`;
  pool.query(sql, (err, data) => {
    if (err) {
      next(err);
      return;
    }
  });

  //简历一个smtp连接
  let transporter = nodemailer.createTransport({
    host: 'smtp.163.com',
    secureConnection: true,
    port: 465,
    auth: {
      user: 'alili8029@163.com',
      pass: 'JYTQLQCKHBLWTJDW',
    },
  });
  //配置相关参数
  let options = {
    from: 'alili8029@163.com',
    to: `${email},alili8029@163.com`,
    //邮箱标题
    subject: 'ZDY宠物寄养有限责任公司',
    //使用模板字符串书写
    html: `<!DOCTYPE html>
   <html>
   <head>
     <meta charset="UTF-8">
     <title>Document</title>
     <style></style>
   </head>
   <body>
     <div class="center">
       <p>尊敬的用户您好:</p>
       <p>欢迎您注册称为zdy寄养宠物有限责任公司的会员用户,您的验证码是${code}，请注意查收。</p>
     </div>
   </body>
   </html>`,
  };
  transporter.sendMail(options, function (err, msg) {
    if (err) {
      console.log(err);
    } else {
      res.send(msg);
    }
  });
});
// 4.验证邮箱验证码是否正确
r.post('/checkcode', (req, res, next) => {
  let obj = req.body;
  let code = obj.code;
  let email = obj.uEmail;
  let sql = `SELECT * FROM user_code WHERE u_email='${email}' AND u_code = '${code}'`;
  console.log(sql);
  pool.query(sql, (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.length == 0) {
      res.send({ code: '201', msg: '验证码错误' });
    } else {
      res.send({ code: '200', msg: '验证码正确' });
    }
  });
});
// 5. 登录
r.post('/login', (req, res, next) => {
  let obj = req.body;
  let email = obj.uEmail;
  let pwd = obj.uPwd;
  let sql = ` SELECT * FROM user WHERE u_email = ? AND u_pwd = ?`;
  pool.query(sql, [email, pwd], (err, data) => {
    if (err) {
      next(err);
      return;
    }

    console.log(data);

    if (data.length == 0) {
      res.send({ code: '201', msg: '登录失败，用户信息不存在！' });
    } else {
      const token = generateToken(data[0].u_id, 2);
      res.send({
        code: 200,
        msg: '登录成功!',
        token: token,
        u_id: data[0].u_id,
        u_name: data[0].u_name,
      });
    }
  });
});

// 5. 查询用用户的联系方式
r.post('/contact', (req, res, next) => {
  let obj = req.body;
  let user_id = obj.user_id;
  let sql = `SELECT * FROM contact WHERE user_id=${user_id}`;
  // console.log(sql)
  pool.query(sql, (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.length == 0) {
      res.send({ code: '201', msg: '没有联系方式,请新增' });
    } else {
      res.send({ code: '200', msg: '查询成功', data: data });
    }
  });
});
// 6通过id查询联系人信息
r.post('/contact/select', (req, res, next) => {
  let obj = req.body;
  let manid = obj.manid;
  let sql = `SELECT * FROM contact WHERE id=${manid}`;
  pool.query(sql, (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.length == 0) {
      res.send({ code: '201', msg: '查询失败' });
    } else {
      res.send({ code: '200', msg: '查询成功', data: data });
    }
  });
});

// 7.更改联系人信息
r.put('/changelinkman', (req, res, next) => {
  let id = req.body.id;
  let name = req.body.name;
  let tel = req.body.tel;
  let sql = `UPDATE contact SET name="${name}",tel="${tel}" WHERE id=${id}`;
  pool.query(sql, [name, tel, id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '修改成功!' });
    } else {
      res.send({ code: '201', msg: '修改失败!' });
      return;
    }
  });
});
// 8.新增联系人信息
r.put('/addlinkman', (req, res, next) => {
  let userid = req.body.userid;
  let name = req.body.name;
  let tel = req.body.tel;
  let sql = `INSERT INTO contact value (?,?,?,?,?)`;
  pool.query(sql, [, userid, name, tel, ,], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '新增联系人成功!' });
    } else {
      res.send({ code: '201', msg: '新增失败!' });
      return;
    }
  });
});

// 9.删除联系人信息
r.post('/deletelinkman', (req, res, next) => {
  let manid = req.body.manid;
  let sql = `DELETE FROM contact WHERE id=${manid}`;
  pool.query(sql, [manid], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.affectedRows == 1) {
      res.send({ code: '200', msg: '删除成功!' });
    } else {
      res.send({ code: '201', msg: '删除失败!' });
      return;
    }
  });
});

/*******************************************/
//导出
module.exports = r;
